package me.zhengjie.modules.business.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import me.zhengjie.exception.RuleException;
import me.zhengjie.modules.business.constant.EnableEnum;
import me.zhengjie.modules.business.constant.ExStatusEnum;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

/**
 * 实验主表。使用逻辑外键关系，懒加载相关表。但是需要注意：必须通过dto的方式，解除无限循环的外键写法问题。
 * 其他方式我测试过，再调用.getFlags()的时候，会出现无线循环，但是没办法避免手动去调用这个get.
 * */
@Entity
@Data
@Table(name="experiment")
public class ExperimentDO implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @ApiModelProperty(value = "实验Key")
    private String id;

    @Column(name = "name")
    @ApiModelProperty(value = "名称")
    private String name;

    @Column(name = "type")
    @ApiModelProperty(value = "类型")
    private Integer type;

    @Column(name = "remark")
    @ApiModelProperty(value = "描述")
    private String remark;

    @Column(name = "status")
    @ApiModelProperty(value = "状态")
    private Integer status;

    @JoinColumn(name = "layer_id")
    @OneToOne
    private FlowLayerSimpleDO layer;

    @JoinColumn(name="app_key")
    @OneToOne
    private ApplicationSimpleDO app;

    @Column(name = "layer_pro")
    @ApiModelProperty(value = "流量层id")
    private Double layerPro;

    @Column(name = "soft_delete")
    @ApiModelProperty(value = "软删除")
    private Boolean softDelete;

    @Column(name = "create_by")
    @ApiModelProperty(value = "createBy")
    private String createBy;

    @Column(name = "update_by")
    @ApiModelProperty(value = "updateBy")
    private String updateBy;

    @Column(name = "create_time")
    @CreationTimestamp
    @ApiModelProperty(value = "createTime")
    private Timestamp createTime;

    @Column(name = "update_time")
    @UpdateTimestamp
    @ApiModelProperty(value = "updateTime")
    private Timestamp updateTime;

    @Column(name = "default_link")
    @ApiModelProperty(value = "默认连接")
    private String defaultLink;

    @Column(name = "pattern_str")
    @ApiModelProperty(value = "匹配规则")
    private String patternStr;

    @Column(name = "pattern_type")
    @ApiModelProperty(value = "匹配模式")
    private int patternType;

    @Column(name = "default_var")
    @ApiModelProperty(value = "默认变量名")
    private String defaultVar;

//    @Column(name = "app_key")
//    @ApiModelProperty(value = "应用ID")
//    private String appKey;



    @Column(name = "confidence")
    @ApiModelProperty(value = "置信度, 默认0.05")
    private double confidence;

    @Column(name = "power")
    @ApiModelProperty(value = "统计功效")
    private double power;

    @OneToMany(mappedBy = "experimentDO",cascade={CascadeType.PERSIST,CascadeType.REMOVE})
    private List<ExFlagDO> flags;

    @OneToMany(mappedBy = "experimentDO",cascade={CascadeType.PERSIST,CascadeType.REMOVE})
    private List<ExPointDO> points;

    public void copy(ExperimentDO source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
    public void init(){
//        setStatus(ExStatusEnum.INIT.value());
        setSoftDelete(false);
        if(getConfidence()<0 || getConfidence()>1){
            throw new RuleException("Confidence",String.valueOf(getConfidence()));
        }
        if(getPower()<0 || getPower()>1){
            throw new RuleException("Confidence",String.valueOf(getPower()));
        }
        if(getLayer()==null){
            throw new RuleException("Layer");
        }
        if(getLayerPro()<0 || getLayerPro()>1){
            throw new RuleException("layerPro",String.valueOf(getLayerPro()));
        }
    }
}